<?php
/**
 * 登录日志
 */
namespace app\admin\controller;

use app\admin\model\LoginLog as LoginLogModel;

class LoginLog extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($search = '', $val = '')
    {
        //$search = !empty($search) ? trim($search) : '';
        //$val    = !empty($val) ? trim($val) : '';
        $this->assign([
            'search' => $search,
            'val'    => $val,
        ]);
        return $this->fetch();
    }

    public function ajaxlist($search = '', $val = '', $page = 1, $limit = '')
    {
        $where  = '';
        $search = !empty($search) ? trim($search) : '';
        $val    = !empty($val) ? trim($val) : '';
        $limit  = !empty($limit) ? trim($limit) : config('webconfig.page');

        if ($search == 'user' && $val) {
            $userid  = \Db::name('user')->where([['user_name', '=', $val]])->value('id');
            $where[] = ['user_id', '=', $userid];
        } else if ($search == 'ip' && $val) {
            $where[] = ['ip', '=', $val];
        }
        $page = !empty($page) ? $page : 1;

        $list  = LoginLogModel::where($where)->page($page, $limit)->order('id','desc')->select();
        $count = LoginLogModel::where($where)->count();
        foreach ($list as $k => $v) {
            $list[$k]['user']  = $v->username->user_name;
            $list[$k]['nickname']  = $v->username->nickname;
            $list[$k]['update_time']  = date('Y-m-d H:i:s',$v['update_time']);
        }
        $list = ["code" => 0, "msg" => "加载中", "count" => $count, "data" => $list];
        return json($list);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function deleteAll($ids = '')
    {
        $ids  = !empty($ids) ? $ids : '';
        $data = LoginLogModel::destroy($ids);
        if (!empty($data)) {
            return $this->success('批量删去成功', 'index');
        }
    }
}
